package com.example.fan.airline_reservation;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.fan.airline_reservation.Model.USER;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class register extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        /*云端数据库连接*/
        Bmob.initialize(this, "0802cbf346296dc9fac5a477f911ba39");

        /*注册按钮监听器*/
        Button register = (Button)findViewById(R.id.register);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toRegister();
            }
        });
    }

    /*点击注册后的相关操作*/
    public void toRegister(){
        EditText username=(EditText)findViewById(R.id.username);
        String myname=username.getText().toString();
        EditText password=(EditText)findViewById(R.id.password);
        String mypassword=password.getText().toString();
        RadioGroup usertype=(RadioGroup)findViewById(R.id.type);
        int type=0;
        switch (usertype.getCheckedRadioButtonId()){
            case R.id.airline:  type=1; break;
        }
        if (myname.length()!=0 && mypassword.length()!=0) {
            /*上传用户信息的代码*/
            final USER Auser = new USER(myname, mypassword, type);
            Auser.save(new SaveListener<String>() {
                @Override
                public void done(String s, BmobException e) {
                    if (e == null) {
                        ToastShow("成功!");
                    }
                }
            });

            /* 弹出一个提示框，点击OK进入相应的页面*/
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("注册成功");
            dialog.setMessage("恭喜你注册成功!");
            dialog.setCancelable(false);
            dialog.setPositiveButton("OK！", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (Auser.getType() == 1) {
                        Intent intent = new Intent(register.this, AC_Interface.class);
                        startActivity(intent);
                    } else if (Auser.getType() == 0) {
                        Intent intent = new Intent(register.this, Traveler_Interface.class);
                        intent.putExtra("USER11", Auser);
                        startActivity(intent);
                    }
                    finish();
                }
            });
            dialog.show();
        }else {
            ToastShow("请确认注册信息！");
        }
    }

    public void ToastShow(String message){
        Toast.makeText(register.this,message,Toast.LENGTH_SHORT).show();
    }

    public void backto_signin(View view) {
        Intent intent=new Intent(register.this,Sign_in.class);
        startActivity(intent);
    }
}
